<?php
// +----------------------------------------------------------------------
// | OneThink [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: 麦当苗儿 <zuojiazi@vip.qq.com> <http://www.zjzit.cn>
// +----------------------------------------------------------------------

namespace Card\Controller;
use Card\Controller\PublicController;

/**
 * 贺卡
 */
class BlessCardController extends PublicController {

    /**
     * 贺卡标题
     */
    public function CardTitle(){
        $data = inputJson();

        // $data['phpsid'] = 'otb9a50k6h10r90ks9ffcro20r';

        //是否登录
        $res = $this->is_login($data['phpsid']);
        if(!$res){
            abnormalOutputJson(['info'=>'请先登录']);
        }

        //查用户id
        $user_id = $this->get_userId($data['phpsid']);
        if($user_id === false){
            errorOutputJson('e'.__LINE__);
        }elseif ($res === []) {
            abnormalOutputJson(['info'=>'请先登录','type'=>1]);//传了空session_id或查不到
        }
        //查名片id
        $card_id = $this->getCardId($data['phpsid']);//名片id
        if($card_id === -1){
            abnormalOutputJson(['info'=>'请先登录','type'=>1]);
        }elseif ($card_id === false) {
            errorOutputJson('e'.__LINE__);
        }elseif(empty($card_id)){
            $card_id = -1;
        }

        //查贺卡标题
        $GreetingCard = D('GreetingCard');
        $res = $GreetingCard->getAllGreetInfo(array('user_id'=>$user_id,'delete_state'=>0),'id,title');
        if($res === false){
            errorOutputJson('h'.__LINE__);
        }elseif(empty($res)){
            successOutputJson([],['card_id'=>$card_id]);
        }else{
            successOutputJson($res,['card_id'=>$card_id]);
        }
        
    }

    /**
     * 编辑标题
     * 
     */
    public function EditCardTitle(){
        $data = inputJson();

        // $data['id'] = 6;
        // $data['title'] = 'hhh123';
        // $data['phpsid'] = 'ofvbs70duforigpk5jcookors0';


        //是否登录
        $res = $this->is_login($data['phpsid']);
        if(!$res){
            abnormalOutputJson(['info'=>'请先登录','type'=>1]);
        }
        if(empty($data['id'])){
            abnormalOutputJson(['info'=>'参数错误','type'=>2]);
        }
        if(empty($data['title'])){
            abnormalOutputJson(['info'=>'标题不能为空','type'=>3]);
        }

        $gdata['id'] = $data['id'];
        $gdata['title'] = $data['title'];
        $GreetingCard = D('GreetingCard');
        $res = $GreetingCard->save($gdata);
        
        if($res === false){
            errorOutputJson('h'.__LINE__);
        }else{
            successOutputJson();
        }
        
    }
     /**
     * 删除标题
     */
    public function DeleteCardTitle(){
        $data = inputJson();

        // $data['id'] = 233;
        // $data['phpsid'] = 'otb9a50k6h10r90ks9ffcro20r';

        if(empty($data['id'])){
            abnormalOutputJson(['info'=>'参数错误','type'=>2]);
        }
        //是否登录
        $res = $this->is_login($data['phpsid']);
        if(!$res){
            abnormalOutputJson(['info'=>'请先登录','type'=>1]);
        }
         //查用户id
        $user_id = $this->get_userId($data['phpsid']);
        if($user_id === false){
            errorOutputJson('h'.__LINE__);
        }elseif ($res === []) {
            abnormalOutputJson(['info'=>'请先登录','type'=>1]);//传了空session_id或查不到
        }

        //是否本人
        $wh['id'] = $data['id'];
        $wh['user_id'] = $user_id;
        $GreetingCard = D('GreetingCard');
        $res = $GreetingCard->where($wh)->find();
        if(empty($res)){
            abnormalOutputJson(['info'=>'非法操作','type'=>3]);
        }
        $dedata['delete_state'] = 1;
        $res = $GreetingCard->where($wh)->save($dedata);
        if($res === false){
            errorOutputJson('h'.__LINE__);
        }else{
            successOutputJson();
        }
        
    }
    /**
     * 创建新贺卡-新增标题
     */
    public function addCardTitle(){
        $data = inputJson();

        // $data['phpsid'] = 'ofvbs70duforigpk5jcookors0';
        // $data['title'] = '贺卡7';
        
        //是否登录
        $res = $this->is_login($data['phpsid']);
        if(!$res){
            abnormalOutputJson(['info'=>'请先登录','type'=>1]);
        }

        //查用户id
        $user_id = $this->get_userId($data['phpsid']);
        if($user_id === false){
            errorOutputJson('h'.__LINE__);
        }elseif ($res === []) {
            abnormalOutputJson(['info'=>'请先登录','type'=>1]);//传了空session_id或查不到
        }

        //增加一张贺卡(存标题信息)
        $cdata['user_id'] = $user_id;
        $cdata['title'] = $data['title'];
        $GreetingCard = D('GreetingCard');
        $GreetingCard->sx_data=$cdata;
        $res = $GreetingCard->addGreet();
        if(isset($res['msg'])){
            abnormalOutputJson(['info'=>$res['msg'],'type'=>2]);//标题为空
        }elseif(!$res){
            errorOutputJson('h'.__LINE__);
        }else{
            successOutputJson($res);
        }
        
    }
    /**
     * 贺卡初始化(编辑页 要判断是否登录)
     */
    public function editCardInit(){
        $data = inputJson();

        // $data['phpsid'] = '13hga3tn6o2v82smdmm2a0r9a4';
        // $data['id'] = 81;
        // $data['is_edit'] = 0;//是否编辑页

        $sres = $this->is_login($data['phpsid']);
        if(!$sres){
            if($data['is_edit'] == 1){
                //如果是编辑页要判断是否登录
                abnormalOutputJson(['info'=>'请先登录','type'=>1]);
            }else{
                abnormalOutputJson(['info'=>'贺卡不存在','type'=>2]);
            }
        }

        //查询贺卡信息
        $user_id = $this->get_userId($data['phpsid']);
        $condition['user_id'] = $user_id;
        $condition['id'] = $data['id'];
        $res = D('GreetingCard')->getGreetInfo($condition);

        $rres = [];

         //查字体颜色
        $color_res = D('BackgroundColor')->where(['id'=>$res['background_color']])->find();
        $word_color = $color_res['word_color'];
        $rres['word_color'] = $word_color;

        if(empty($res)){
            if($data['is_edit'] == 1){
                //如果是编辑页要判断是否本人操作
                abnormalOutputJson(['info'=>'参数错误','type'=>3]);//不是本人操作
            }else{
                abnormalOutputJson(['info'=>'贺卡不存在','type'=>2]);//不是本人操作
            }
        }

         //贺卡标题
        $rres['btitle'] = $res['title'];

        //查姓名
        $cart_id = $this->getCardId($data['phpsid']);//获取名片id
        if($cart_id === false){
            errorOutputJson('e'.__LINE__);
        }elseif ($cart_id === []) {
            $user_name = '';//没有名字
        }

        $Picture = D('Picture');
        //查中间图片路径
        if(!empty($res['img_id'])){
            $pinfo = $Picture->ImgInfo($res['img_id']);
            if($pinfo === false){
                errorOutputJson('e'.__LINE__);
            }elseif ($pinfo === []) {
                $rres['img']['id'] = '';
                $rres['img']['path'] = '';
            }else{
                $rres['img'] = $pinfo;
            }
        }else{
            $rres['img']['id'] = '';
            $rres['img']['path'] = '';
        }
        //查背景图 模板id 和图片路径
        $rres['backgimg'] = $this->getTemplate($res['background_color'],'backgimg','BackgroundColor',$Picture,'getDefaultBacktemp');
        //查贺卡模板(装饰) 模板id 和图片路径
        $rres['Ctemplimg'] = $this->getTemplate($res['template_id'],'Ctemplimg','CardTemplate',$Picture,'getDefaultCardtemp');

        //查祝福语(文字/图片)
        if(!empty($res['word_template'])){//图片
            $is_pic = 1;
            $wres = D('WordTemplate')->find($res['word_template']);
            if(!empty($wres['img_id'])){//查模板图路径
                $pinfo = $Picture->ImgInfo($wres['img_id']);
                if($pinfo === false){
                    errorOutputJson('e'.__LINE__);
                }elseif ($pinfo === []) {
                    $rres['Wtemplimg']['id'] = '';
                    $rres['Wtemplimg']['path'] = '';
                }else{
                    $rres['Wtemplimg']['id'] = $res['word_template'];
                    $rres['Wtemplimg']['path'] = $pinfo['path'];
                }
            }else{
                $rres['Wtemplimg']['id'] = '';
                $rres['Wtemplimg']['path'] = '';
            }
            $rres['content'] = '';
        }elseif(!empty($res['content'])){
            $is_pic = 0;
            $rres['content'] = $res['content'];
            $rres['Wtemplimg']['id'] = '';
            $rres['Wtemplimg']['path'] = '';
        }else{
            $is_pic = 3;
            $rres['content'] = '';
            $rres['Wtemplimg']['id'] = '';
            $rres['Wtemplimg']['path'] = '';
        }
        //查名片二维码路径
        $card_id = $this->getCardId($data['phpsid']);//名片id
        $rres['Qcode_path'] = C('CARD_DOMAIN').$this->ChooseMakeqrurl($card_id,$data['id']);

        successOutputJson($rres,['is_pic'=>$is_pic]);
        
    }
    /**
     * 贺卡初始化(分享页)
     */
    public function shareCardInit(){
        $data = inputJson();

        // $data['id'] = 81;//贺卡id
        // $data['card_id'] = 7;
        // $data['phpsid'] = '13hga3tn6o2v82smdmm2a0r9a44';

        if(empty($data['id']) || empty($data['card_id'])){
            abnormalOutputJson(['info'=>'参数错误','type'=>1]);
        }
        //查询贺卡信息
        $res = D('GreetingCard')->find($data['id']);

        //是否指引过 0-没有 1-有
        $state = intval($res['state']);

        if(empty($res)){
            abnormalOutputJson(['info'=>'贺卡不存在','type'=>2]);
        }
        $user_id = $res['user_id'];

        if(empty($data['phpsid'])){
            $is_oneself = 0;
        }else{
            //查询贺卡信息
            $caozuo_user_id = $this->get_userId($data['phpsid']);
            if($caozuo_user_id === false){
                errorOutputJson('e'.__LINE__);
            }elseif ($caozuo_user_id === []) {
                $is_oneself = 0;
            }else{
                $condition['user_id'] = $caozuo_user_id;
                $condition['id'] = $data['id'];
                $userres = D('GreetingCard')->getGreetInfo($condition);
                if(empty($userres)){
                    $is_oneself = 0;
                }else{
                    $is_oneself = 1;
                }
            }
            
        }

        $rres = [];

         //贺卡标题
        $rres['btitle'] = $res['title'];

        //查姓名
        $nameData = D('CardMember')->where(array('id'=>$user_id))->find();
        if(!empty($nameData['name'])){
            $name = $nameData['name'];
        }elseif(!empty($nameData['wechat'])){
            $result = mall_json_post_http_curl('act=wx_login&op=getWXLoginInfo', ['openid' => $nameData['wechat']]);
            $name = $result['data']['nickname'];
        }else{
            $name = '好友';
        }

        $rres['name'] = $name;

        $Picture = D('Picture');
        //查中间图片路径
        if(!empty($res['img_id'])){
            $pinfo = $Picture->ImgInfo($res['img_id']);
            if($pinfo === false){
                errorOutputJson('e'.__LINE__);
            }elseif ($pinfo === []) {
                $rres['img']['id'] = '';
                $rres['img']['path'] = '';
            }else{
                $rres['img'] = $pinfo;
            }
        }else{
            $rres['img']['id'] = '';
            $rres['img']['path'] = '';
        }
        //查背景图 模板id 和图片路径
        $rres['backgimg'] = $this->getTemplate($res['background_color'],'backgimg','BackgroundColor',$Picture,'getDefaultBacktemp');
        //查贺卡模板(装饰) 模板id 和图片路径
        $rres['Ctemplimg'] = $this->getTemplate($res['template_id'],'Ctemplimg','CardTemplate',$Picture,'getDefaultCardtemp');

        //查祝福语(文字/图片)
        if(!empty($res['word_template'])){//图片
            $is_pic = 1;
            $wres = D('WordTemplate')->find($res['word_template']);
            if(!empty($wres['img_id'])){//查模板图路径
                $pinfo = $Picture->ImgInfo($wres['img_id']);
                if($pinfo === false){
                    errorOutputJson('e'.__LINE__);
                }elseif ($pinfo === []) {
                    $rres['Wtemplimg']['id'] = '';
                    $rres['Wtemplimg']['path'] = '';
                }else{
                    $rres['Wtemplimg']['id'] = $res['word_template'];
                    $rres['Wtemplimg']['path'] = $pinfo['path'];
                }
            }else{
                $rres['Wtemplimg']['id'] = '';
                $rres['Wtemplimg']['path'] = '';
            }
            $rres['content'] = '';
        }elseif(!empty($res['content'])){
            $is_pic = 0;
            $rres['content'] = $res['content'];
            $rres['Wtemplimg']['id'] = '';
            $rres['Wtemplimg']['path'] = '';
        }else{
            $is_pic = 3;
            $rres['content'] = '';
            $rres['Wtemplimg']['id'] = '';
            $rres['Wtemplimg']['path'] = '';
        }

        //查字体颜色
        $color_res = D('BackgroundColor')->where(['id'=>$res['background_color']])->find();
        $word_color = $color_res['word_color'];
        $rres['word_color'] = $word_color;

        //查名片二维码路径
        $card_id = $data['card_id'];//名片id
        $rres['Qcode_path'] = C('CARD_DOMAIN').$this->ChooseMakeqrurl($card_id,$data['id']);

        if(empty($card_id)){
            $make_path = C('CHOOSEMAKE').'/'.$data['id'].'/-1'; //没有名片的跳转链接
        }else{
            //云名片信息
            $dao = M('User');
            $info = $dao->find($card_id);
            if (empty($info)) {
                $make_path = C('CHOOSEMAKE').'/'.$data['id'].'/-1'; //没有名片的跳转链接
            }else{
                $make_path = C('CHOOSEMAKE').'/'.$data['id'].'/'.$card_id; //有名片的跳转链接
            }
        }
        $rres['make_path'] = $make_path;

        // var_dump($rres);

        successOutputJson($rres,['is_pic'=>$is_pic,'is_oneself'=>$is_oneself,'state'=>$state]);
        
    }
    /**
     * 获取模板信息
     */
    private function getTemplate($UserTempId,$name,$Model,$Picture,$defaultName){
        //用户没有添加模板时直接用默认模板
        if(empty($UserTempId)){
            $defaulttemp = $this->$defaultName();
            $pinfo = $Picture->ImgInfo($defaulttemp['img_id']);
            if($pinfo === false){
                errorOutputJson('e'.__LINE__);
            }elseif ($pinfo === []) {
                $rres[$name]['id'] = '';
                $rres[$name]['path'] = '';
            }else{
                $rres[$name]['id'] = $defaulttemp['id'];
                $rres[$name]['path'] = $pinfo['path'];
            }
        }else{
            //用户有添加模板时用用户添加的模板
            $bres = D($Model)->find($UserTempId);
            if(!empty($bres['img_id'])){//查背景图路径
                $pinfo = $Picture->ImgInfo($bres['img_id']);
                if($pinfo === false){
                    errorOutputJson('e'.__LINE__);
                }elseif ($pinfo === []) {
                    $rres[$name]['id'] = '';
                    $rres[$name]['path'] = '';
                }else{
                    $rres[$name]['id'] = $UserTempId;
                    $rres[$name]['path'] = $pinfo['path'];
                }
            }else{
                $rres[$name]['id'] = '';
                $rres[$name]['path'] = '';
            }
        }
        return $rres[$name];
    }
     
    /**
     * 查询贺卡模板
     */
    public function getCardTemplate(){
        $data = inputJson();

        // $data['phpsid'] = 'otb9a50k6h10r90ks9ffcro20r';
        // $data['page'] = 1;//3页
        // $data['type'] = 1;

        $data['rows'] = 6;

        //是否登录
        $res = $this->is_login($data['phpsid']);
        if(!$res){
            abnormalOutputJson(['info'=>'请先登录']);
        }

        //查询模板
        $Picture = D('Picture');
        $CardTemplate = D('CardTemplate');

        $num_condition['state'] = 1;
        if(($data['type'] !== -1)){
            $num_condition['classification'] = $data['type'];
        }

        $num = $CardTemplate->where($num_condition)->count();//模板总数

        $start = ($data['page']-1)*$data['rows'];
        $limit = $start.','.$data['rows'];
        $res = $CardTemplate->getAllTemplate($limit,'*','is_default desc,sort desc,id desc',$num_condition);
        if($res === false){
            errorOutputJson('h'.__LINE__);
        }elseif (empty($res)) { //没有模板
            successOutputJson([]);
        }
        foreach ($res as $k => $v) {
            //查贺卡模板图片路径
            $Picture = D('Picture');
            if(!empty($v['img_id'])){
                $pinfo = $Picture->ImgInfo($v['img_id']);
                if($pinfo === false){
                    errorOutputJson('h'.__LINE__);
                }elseif ($pinfo === []) {
                    $res[$k]['big_img_path'] = '';
                    $res[$k]['small_img_path'] = '';
                }else{
                    $res[$k]['big_img_path'] = $pinfo['path'];
                    $res[$k]['small_img_path'] = $pinfo['path'].'.s.jpg';
                }
            }else{
                $res[$k]['big_img_path'] = '';
                $res[$k]['small_img_path'] = '';
            }
        }

        // var_dump($res);

        successOutputJson($res,['total'=>$num]);
        
    }
    // public function getCardTemplate(){
    //     $data = inputJson();

    //     $data['phpsid'] = 'otb9a50k6h10r90ks9ffcro20r';
        
    //     //是否登录
    //     $res = $this->is_login($data['phpsid']);
    //     if(!$res){
    //         abnormalOutputJson(['info'=>'请先登录']);
    //     }

    //     //查询模板
    //     $Picture = D('Picture');
    //     $CardTemplate = D('CardTemplate');
    //     $num = $CardTemplate->where(array('state'=>1))->count();//模板总数
    //     $inum = ceil($num/1);
    //     $result = [];
    //     for ($i=0; $i < $inum; $i++) { 
    //         $rows = 1;
    //         $start = $i*$rows;
    //         $limit = $start.','.$rows;
    //         $res = $CardTemplate->getAllTemplate($limit,'*','is_default desc,id desc',array('state'=>1));
    //         if($res === false){
    //             errorOutputJson('h'.__LINE__);
    //         }elseif (empty($res)) { //没有模板
    //             successOutputJson([]);
    //         }
    //         foreach ($res as $k => $v) {
    //             //查贺卡模板图片路径
    //             $Picture = D('Picture');
    //             if(!empty($v['img_id'])){
    //                 $pinfo = $Picture->ImgInfo($v['img_id']);
    //                 if($pinfo === false){
    //                     errorOutputJson('h'.__LINE__);
    //                 }elseif ($pinfo === []) {
    //                     $res[$k]['big_img_path'] = '';
    //                     $res[$k]['small_img_path'] = '';
    //                 }else{
    //                     $res[$k]['big_img_path'] = $pinfo['path'];
    //                     $res[$k]['small_img_path'] = $pinfo['path'].'.s.jpg';
    //                 }
    //             }else{
    //                 $res[$k]['big_img_path'] = '';
    //                 $res[$k]['small_img_path'] = '';
    //             }
    //         }
    //         $result[$i] = $res;
    //         unset($res);
    //     }

    //     // var_dump($result);

    //     successOutputJson($result);
        
    // }

    /**
     * 查询背景
     */
    public function getBackgroundColor(){
        $data = inputJson();

        // $data['phpsid'] = 'otb9a50k6h10r90ks9ffcro20r';
        // $data['page'] = 1;
        // $data['type'] = 1;

        $data['rows'] = 6;
        
        //是否登录
        $res = $this->is_login($data['phpsid']);
        if(!$res){
            abnormalOutputJson(['info'=>'请先登录']);
        }
        
         //查询模板
        $Picture = D('Picture');
        $BackgroundColor = D('BackgroundColor');

        $num_condition['state'] = 1;
        if(($data['type'] !== -1)){
            $num_condition['classification'] = $data['type'];
        }
        $num = $BackgroundColor->where($num_condition)->count();//模板总数

        $start = ($data['page']-1)*$data['rows'];
        $limit = $start.','.$data['rows'];
        $res = $BackgroundColor->getAllTemplate($limit,'*','is_default desc,sort desc,id desc',$num_condition);
        if($res === false){
            errorOutputJson('h'.__LINE__);
        }elseif (empty($res)) { //没有模板
            successOutputJson([]);
        }

        foreach ($res as $k => $v) {
            //查贺卡模板图片路径
            if(!empty($v['img_id'])){
                $pinfo = $Picture->ImgInfo($v['img_id']);
                if($pinfo === false){
                    errorOutputJson('h'.__LINE__);
                }elseif ($pinfo === []) {
                    $res[$k]['big_img_path'] = '';
                    $res[$k]['small_img_path'] = '';
                }else{
                    $res[$k]['big_img_path'] = $pinfo['path'];
                    $res[$k]['small_img_path'] = $pinfo['path'].'.s.jpg';
                }
            }else{
                $res[$k]['big_img_path'] = '';
                $res[$k]['small_img_path'] = '';
            }   

            //字体颜色
            $res[$k]['word_color'] = $v['word_color'];         
        }


       
        // var_dump($res);        

        successOutputJson($res,['total'=>$num]);
        
    }

    /**
     * 查询文字模板图
     */
    public function getWordTemplate(){
        $data = inputJson();

        // $data['phpsid'] = 'otb9a50k6h10r90ks9ffcro20r';
        // $data['page'] = 2;
        // $data['type'] = 2;

        $data['rows'] = 6;
        
        //是否登录
        $res = $this->is_login($data['phpsid']);
        if(!$res){
            abnormalOutputJson(['info'=>'请先登录']);
        }
        
         //查询模板
        $Picture = D('Picture');
        $WordTemplate = D('WordTemplate');
        $condition['state'] = 1;
        if(($data['type'] !== -1)){
            $condition['classification'] = $data['type'];
        }

        $num = $WordTemplate->where($condition)->count();//模板总数
        
        $start = ($data['page']-1)*$data['rows'];
        $limit = $start.','.$data['rows'];
        $res = $WordTemplate->getAllTemplate($condition,$limit);
        if($res === false){
            errorOutputJson('h'.__LINE__);
        }elseif (empty($res)) { //没有模板
            successOutputJson([]);
        }
        foreach ($res as $k => $v) {
            //查贺卡模板图片路径
            $Picture = D('Picture');
            if(!empty($v['img_id'])){
                $pinfo = $Picture->ImgInfo($v['img_id']);
                if($pinfo === false){
                    errorOutputJson('h'.__LINE__);
                }elseif ($pinfo === []) {
                    $res[$k]['big_img_path'] = '';
                    $res[$k]['small_img_path'] = '';
                }else{
                    $res[$k]['big_img_path'] = $pinfo['path'];
                    $res[$k]['small_img_path'] = $pinfo['path'];
                }
            }else{
                $res[$k]['big_img_path'] = '';
                $res[$k]['small_img_path'] = '';
            }

        }

        // var_dump($res);

        successOutputJson($res,['total'=>$num]);
        
    }

    /**
     * 编辑贺卡
     */
    public function editCard(){
        $data = inputJson();

        // $data['phpsid'] = '0cf2o9k0a38uv1gfmfi846p5l4';
        // $data['id'] = 6;
        // $data['template_id'] = 888;
        // $data['img_id'] = 6;
        // $data['word_template'] = 6;//文字模板
        // $data['background_color'] = 6;//颜色模板
        // $data['content'] = 6;
        
        if(!empty($data['content'])){
            $data['content'] = replaceSpecialChar($data['content']);
            $data['content'] = filterEmoji($data['content'],'?');
        }
        if(empty($data['id'])){
            abnormalOutputJson(['info'=>'参数错误','type'=>2]);
        }
        //是否登录
        $res = $this->is_login($data['phpsid']);
        if(!$res){
            abnormalOutputJson(['info'=>'请先登录','type'=>1]);
        }
        //判断操作的是否本人
        $user_id = $this->get_userId($data['phpsid']);
        if($user_id === false){
            errorOutputJson('e'.__LINE__);
        }elseif ($user_id === []) {
            abnormalOutputJson(['info'=>'请先登录','type'=>1]);
        }
        $condition['user_id'] = $user_id;
        $condition['id'] = $data['id'];

        $res = D('GreetingCard')->getGreetInfo($condition);
        if(empty($res)){
            abnormalOutputJson(['info'=>'参数错误','type'=>2]);//不是本人操作
        }
        if(empty($data['template_id'])){
            abnormalOutputJson(['info'=>'请选择一个模板','type'=>3]);
        }
        if(empty($data['img_id'])){
            abnormalOutputJson(['info'=>'请上传照片','type'=>4]);
        }
        if(empty($data['content']) && empty($data['word_template'])){
            abnormalOutputJson(['info'=>'请填写祝福语','type'=>5]);
        }
        if(empty($data['background_color'])){
            abnormalOutputJson(['info'=>'请选择背景','type'=>6]);
        }
        if(!empty($data['content']) && !empty($data['word_template'])){
            abnormalOutputJson(['info'=>'祝福语只能填写文字或选择图片，不能同时提交','type'=>7]);
        }

        //编辑贺卡信息
        $cdata['id'] = $data['id'];
        $cdata['template_id'] = $data['template_id'];
        $cdata['img_id'] = $data['img_id'];
        if(!empty($data['word_template'])){
            $cdata['word_template'] = $data['word_template'];
            $cdata['content'] = '';
        }
        if(!empty($data['content'])){
            $cdata['content'] = $data['content'];
            $cdata['word_template'] = 0;
        }
        $cdata['background_color'] = $data['background_color'];
        $cdata['utime'] = time();

        $GreetingCard = D('GreetingCard');
        $res = $GreetingCard->save($cdata);
        if($res === false){
            errorOutputJson('e'.__LINE__);
        }else{
            //查名片id
            $card_id = $this->getCardId($data['phpsid']);//获取名片id
            if($card_id === false){
                errorOutputJson('e'.__LINE__);
            }elseif ($card_id === -1) {
                $card_id = -1;
            }elseif ($card_id === []) {
                $card_id = -1;
            }
            $blessCard_id = $data['id'];

            successOutputJson(['card_id'=>$card_id,'blessCard_id'=>$blessCard_id]);
        }
        
    }

    /**
     * 实时编辑贺卡
     */
    public function ImmediateEdit(){

        $data = inputJson();

        // $data['phpsid'] = 'otb9a50k6h10r90ks9ffcro20r';
        // $data['id'] = 83;//贺卡id
        // $data['template_id'] = 888;
        // $data['img_id'] = 6;
        // $data['word_template'] = 6;//文字模板
        // $data['background_color'] = '';//颜色模板
        // $data['content'] = 6;
        
        //处理在自定义祝福语上传特殊字符的情况
        if(isset($data['content']) && !empty($data['content'])){
            $data['content'] = replaceSpecialChar($data['content']);
            $data['content'] = filterEmoji($data['content'],'?');
        }
        //祝福语只能选自定义或图片
        if(isset($data['word_template']) && !empty($data['word_template'])){
            $data['content'] = '';
        }elseif(isset($data['content']) && !empty($data['content'])){
            $data['word_template'] = 0;
        }

        //编辑贺卡信息
        $data['utime'] = time();

        $GreetingCard = D('GreetingCard');
        $res = $GreetingCard->save($data);
        if($res === false){
            errorOutputJson('e'.__LINE__);
        }else{
            //查名片id
            $card_id = $this->getCardId($data['phpsid']);//获取名片id
            if($card_id === false){
                errorOutputJson('e'.__LINE__);
            }elseif ($card_id === -1) {
                $card_id = -1;
            }elseif ($card_id === []) {
                $card_id = -1;
            }
            successOutputJson(['card_id'=>$card_id,'blessCard_id'=>$data['id']]);
        }
    }
    
    /**
     * 贺卡详情指引 点击我知道了 取消指引
     */
    public function CancelTheGuide(){
        $data = inputJson();
        
        // $data['id'] = 83;//贺卡id

        if(empty($data['id'])){
            abnormalOutputJson(['info'=>'参数错误']);
        }
        $data['state'] = 1;
        $res = D('GreetingCard')->save($data);
        if($res === false){
            errorOutputJson('e'.__LINE__);
        }else{
            successOutputJson();
        }

    }

    /**
     * 查询素材的分类
     */
    public function GetBackClassification(){
        //大的排在前面
        $res = D('SmaterialClassification')->order('sort desc')->select();
        // var_dump($res);
        
        successOutputJson($res);

    }
    
}
